<?php
namespace Admin\Model;
use Think\Model;

class AclModel extends Model
{
    public $default_password = 123456;

    /**
     * 取得节点计数或数据
     *
     * @param int $w 得到数据类型：1计数，2数据，3计数&数据，4单条数据
     * @param array $search 筛选条件
     * @param string $fields 需要查询的字段
     * @param int $page 分页
     * @param int $pageSize 分页大小
     * @param string $by 排序方式
     * @param string $exclude 排除条件
     *
     * @return array
     *
     */
    public function getAclNode($w=3, $search=null, $fields='*', $page=null, $pageSize=null, $by='', $exclude=null)
    {
        $where = ' where 1';

        $limit = '';
        if($page && $pageSize){
            $limit = ' limit '.$pageSize.' offset '.($page - 1)*$pageSize;
        }

        $orderby = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderby .= 'node_id desc';
        }else{
            $orderby .= $by;
        }

        if(is_array($search))
        {
            isset($search['node_id']) && intval($search['node_id'])>0 && $where .= ' and node_id='.intval($search['node_id']);
            isset($search['node_ids']) && $search['node_ids']!='' && $where .= ' and node_id in ('.$search['node_ids'].')';
            isset($search['node_pid']) && intval($search['node_pid'])>-1 && $where .= ' and node_pid='.intval($search['node_pid']);
            isset($search['m_c_a']) && $search['m_c_a']!='' && $where .= " and m_c_a='".$search['m_c_a']."'";
            isset($search['st']) && $search['st']!='' && $where .= " and st=".intval($search['st']);
            isset($search['node_level']) && $where .= " and node_level=".intval($search['node_level']);
            isset($search['lt_node_level']) && $where .= " and node_level<".intval($search['lt_node_level']);
            isset($search['ids']) && $search['ids']!='' && $where .= " and node_id in (".$search['ids'].")";
            isset($search['node_pid_path']) && $search['node_pid_path']!='' && $where .= " and node_pid_path like '%".$search['node_pid_path']."%'";
        }

        if($exclude){
            $where .= $exclude;
        }

        $sql_ct = 'select count(node_id) as tot from '.getTable('tb_acl_node').$where;
        $sql    = 'select '.$fields.' from '.getTable('tb_acl_node').$where.$orderby;

        //只得到计数
        if($w === 1)
        {
            $ct = $this->query($sql_ct);
            if($ct === false){
                return 0;
            }else{
                return intval($ct[0]['tot']);
            }
        }

        //只得到数据
        if($w === 2)
        {
            return $this->query($sql.$limit);
        }

        //得到计数 & 数据
        if($w === 3)
        {
            $datas = array();
            $ct = $this->query($sql_ct);
            if($ct === false){
                $datas['tot'] = 0;
            }else{
                $datas['tot'] = intval($ct[0]['tot']);
            }
            $datas['datas'] = $this->query($sql.$limit);
            return $datas;
        }

        //得到一条数据
        if($w === 4)
        {
            $rs = $this->query($sql.' limit 1');
            if($rs === false){
                return false;
            }else{
                return array_shift($rs);
            }
        }

        return false;
    }

    /**
     * 得到权限的checkbox
     *
     * @param int $group_id 分组id
     * @param int $admin_id 用户id
     *
     * @return string
     */
    public function getGroupNodesToCheckbox($group_id=0, $admin_id=0)
    {
        $group_acl = $this->getAclGroup(4, array('group_id'=>$group_id), 'group_acl');
        if( ! $group_acl['group_acl']){ return false; }
        $nodes = $this->getAclNode(2, array('node_ids'=>$group_acl['group_acl']));
        if($admin_id !== 0){
            $admin_id = (int)$admin_id;
            $admin_acl = $this->getAclAdmin(4, array('admin_id'=>$admin_id), 'a.admin_acl');
            if($admin_acl === false){ return false; }
            $chk_nodes = explode(',', $admin_acl['admin_acl']);
        }
        if($nodes){
            $nodes = list_to_tree($nodes, 'node_id', 'node_pid', '_sub');
            $html = '';
            foreach($nodes as $v){
                if($admin_id){$ck = in_array($v['node_id'], $chk_nodes) ? 'checked="checked"' : '';}
                $html .= '<div><input class="cb" type="checkbox" name="auth[]" id="n_'.$v['node_id'].'" value="'.$v['node_id'].'" '.$ck.'><label for="n_'.$v['node_id'].'" class="color_0 fb f14">'.$v['node_name'].'</label>';
                if($v['_sub'])foreach($v['_sub'] as $vv){
                    if($admin_id){$ck = in_array($vv['node_id'], $chk_nodes) ? 'checked="checked"' : '';}
                    $html .= '<div class="mleft30px"><input class="cb" type="checkbox" name="auth[]" id="n_'.$vv['node_id'].'" value="'.$vv['node_id'].'" '.$ck.' /><label for="n_'.$vv['node_id'].'" class="fb">'.$vv['node_name'].'</label>
                            <div class="mleft60px bd1sccc">';
                    if($vv['_sub'])foreach($vv['_sub'] as $vvv){
                        if($admin_id){$ck = in_array($vvv['node_id'], $chk_nodes) ? 'checked="checked"' : '';}
                        $html .= '<input type="checkbox" name="auth[]" id="n_'.$vvv['node_id'].'" value="'.$vvv['node_id'].'" '.$ck.' /><label for="n_'.$vvv['node_id'].'">'.$vvv['node_name'].'</label>&nbsp;&nbsp;&nbsp;';
                    }
                    $html .= '</div>
                        </div>';
                }
                $html .= '</div>';
            }
            return $html;
        }else{
            return false;
        }
    }

    /**
     * 添加节点
     *
     * @param array $datas
     *
     * @return array
     */
    public function addAclNode($datas)
    {
        if(isset($datas['node_name']) && $datas['node_name']!=''){
            $node_name = $datas['node_name'];
        }else{
            return array('st'=>'err', 'msg'=>'请输入节点名称');
        }
        if(isset($datas['m_c_a']) && $datas['m_c_a']!=''){
            $m_c_a = $datas['m_c_a'];
        }else{
            return array('st'=>'err', 'msg'=>'请输入节点地址');
        }
        if(isset($datas['node_pid'])){
            $node_pid = abs(intval($datas['node_pid']));
        }else{
            $node_pid = 0;
        }
        if(isset($datas['node_level'])){
            if(in_array(intval($datas['node_level']), array(1, 2, 3, 4))){
                $node_level = intval($datas['node_level']);
            }else{
                $node_level = 4;
            }
        }else{
            $node_level = 4;
        }
        if(isset($datas['px'])){
            $px = intval($datas['px']);
            if($px < 1){ $px = 1; }
        }else{
            $px = 1;
        }
        //得到节点分级路径
        if($node_pid == 0){
            $node_pid_path = ',0,';
        }else{
            $tmp = $this->getAclNode(4, array('node_id'=>$node_pid, 'node_pid_path'));
            $node_pid_path = $tmp['node_pid_path'].$node_pid.',';
        }

        $row = array();
        $row['node_pid'] = $node_pid;
        $row['node_pid_path'] = $node_pid_path;
        $row['m_c_a'] = $m_c_a;
        $row['node_name'] = $node_name;
        $row['node_level'] = $node_level;
        $row['px'] = $px;

        $insertID = $this->add($row, array('table'=>getTable('tb_acl_node')));

        return array('st'=>'ok', 'msg'=>$insertID);
    }

    /**
     * 修改节点
     *
     * @param array $datas
     *
     * @return array
     */
    public function editAclNode($datas)
    {
        if(isset($datas['node_id']) && intval($datas['node_id'])>0){
            $node_id = $datas['node_id'];
        }else{
            return array('st'=>'err', 'msg'=>'参数错误');
        }
        if(isset($datas['node_name']) && $datas['node_name']!=''){
            $node_name = $datas['node_name'];
        }else{
            return array('st'=>'err', 'msg'=>'请输入节点名称');
        }
        if(isset($datas['m_c_a']) && $datas['m_c_a']!=''){
            $m_c_a = $datas['m_c_a'];
        }else{
            return array('st'=>'err', 'msg'=>'请输入节点地址');
        }
        if(isset($datas['node_level'])){
            if(in_array(intval($datas['node_level']), array(1, 2, 3, 4))){
                $node_level = intval($datas['node_level']);
            }else{
                $node_level = 4;
            }
        }else{
            $node_level = 4;
        }
        if(isset($datas['px'])){
            $px = intval($datas['px']);
            if($px < 1){ $px = 1; }
        }else{
            $px = 1;
        }

        $row = array();
        $row['m_c_a'] = $m_c_a;
        $row['node_name'] = $node_name;
        $row['node_level'] = $node_level;
        $row['px'] = $px;

        $numRows = $this->save($row, array('table'=>getTable('tb_acl_node'), 'where'=>array('node_id'=>$node_id)));
        if($numRows === false){ return array('st'=>'err', 'msg'=>'没有更新'); }
        else{ return array('st'=>'ok', 'msg'=>$numRows); }
    }

    /**
     * 删除节点
     *
     * @param int $node_id
     *
     * @return array
     */
    public function delAclNode($node_id=0)
    {
        $node_id = intval($node_id);
        if($node_id < 1){ return array('st'=>'err', 'msg'=>'参数错误'); }
        //检查有没有子菜单
        $ck = $this->getAclNode(1, array('node_pid_path'=>','.$node_id.','));
        if($ck > 0){ return array('st'=>'err', 'msg'=>'有子菜单，不能删除'); }
        $numRows = $this->delete(array('table'=>getTable('tb_acl_node'), 'where'=>array('node_id'=>$node_id)));
        if($numRows === false){ return array('st'=>'err', 'msg'=>'没有删除'); }
        else{ return array('st'=>'ok', 'msg'=>$numRows); }
    }

    /**
     * 修改节点
     *
     * @param array $dat 需要更新的数据
     * @param int $node_id 节点id
     *
     * @return array
     */
    public function updateAclNodeByNodeId($dat, $node_id=0)
    {
        $node_id = intval($node_id);
        if($node_id < 1){ return array('st'=>'err', 'msg'=>'参数错误'); }
        $numRows = $this->save($dat, array('table'=>getTable('tb_acl_node'), 'where'=>array('node_id'=>$node_id)));
        if($numRows === false){ return array('st'=>'err', 'msg'=>'没有更新'); }
        else{ return array('st'=>'ok', 'msg'=>$numRows); }
    }

    /**
     * 得到分组列表
     *
     * @param int $w
     * @param array $search
     * @param string $fields
     * @param int $page
     * @param int $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getAclGroup($w=3, $search=null, $fields='*', $page=null, $pageSize=null, $by='', $exclude=null)
    {
        $where = ' where 1=1';

        $limit = '';
        if($page && $pageSize){
            $limit = ' limit '.$pageSize.' offset '.($page - 1)*$pageSize;
        }

        $orderby = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderby .= 'group_id desc';
        }else{
            $orderby .= $by;
        }

        if(is_array($search))
        {
            isset($search['group_id']) && intval($search['group_id'])>0 && $where .= ' and group_id='.intval($search['group_id']);
            isset($search['st']) && $search['st']!='' && $where .= ' and st='.intval($search['st']);
            isset($search['group_name']) && $search['group_name']!='' && $where .= " and group_name='".$search['group_name']."'";
            isset($search['group_path']) && $search['group_path']!='' && $where .= " and group_path like '%".$search['group_path']."%'";
        }

        if($exclude){
            $where .= $exclude;
        }

        $sql_ct = "select count(group_id) as tot from ".getTable('tb_acl_group').$where;
        $sql    = "select $fields from ".getTable('tb_acl_group').$where.$orderby;

        //只得到计数
        if($w === 1)
        {
            $ct = $this->query($sql_ct);
            if($ct === false){
                return 0;
            }else{
                return intval($ct[0]['tot']);
            }
        }

        //只得到数据
        if($w === 2)
        {
            return $this->query($sql.$limit);
        }

        //得到计数 & 数据
        if($w === 3)
        {
            $datas = array();
            $ct = $this->query($sql_ct);
            if($ct === false){
                $datas['tot'] = 0;
            }else{
                $datas['tot'] = intval($ct[0]['tot']);
            }
            $datas['datas'] = $this->query($sql.$limit);
            return $datas;
        }

        //得到一条数据
        if($w === 4)
        {
            $rs = $this->query($sql.' limit 1');
            if($rs === false){
                return false;
            }else{
                return array_shift($rs);
            }
        }

        return false;
    }

    /**
     * 得到分组select
     *
     * @param string $choose 默认选中
     * @param string $name 名称、ID
     * @param string $onchange 事件
     * @param boolean $disabled
     *
     * @return string
     */
    public function getAclGroupSelect($choose='0', $name='group_id', $onchange='', $disabled=false)
    {
        $dis = $disabled ? 'disabled="disabled"' : '';
        $html = '<select name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'" '.$dis.'><option value="">--请选择--</option>';
        $list = $this->getAclGroup(2, array('st'=>1), 'group_id,group_pid,group_name,group_depth', null, null, 'group_id asc');
        if($list){
            $list = list_to_tree($list, 'group_id', 'group_pid', '_sub');
            floatArray($list, '_sub', $rs);
            unset($list);
            foreach($rs as $v){
                $sel = ($v['group_id'] == $choose) ? 'selected="selected"' : '';
                $html .= '<option value="'.$v['group_id'].'" '.$sel.'>'.str_repeat('&nbsp;', $v['group_depth']*6).$v['group_name'].'</option>';
            }
        }
        $html .= '</select>';
        return $html;
    }

    /**
     * 添加分组
     *
     * @param array $datas
     *
     * @return array
     */
    public function addAclGroup($datas)
    {
        if(isset($datas['group_name']) && $datas['group_name']!=''){
            $group_name = $datas['group_name'];
        }else{
            return array('st'=>'err', 'msg'=>'请输入分组名称');
        }

        if(isset($datas['group_id']) && intval($datas['group_id'])>0){
            $group_pid = intval($datas['group_id']);
        }else{
            $group_pid = 0;
        }

        $group_acl = '';
        if(is_array($datas['auth']) && count($datas['auth'])){
            $tmp = array();
            foreach($datas['auth'] as $v){
                $v = intval($v);
                if($v > 0){
                    $tmp[] = $v;
                }
            }
            if(count($tmp)){
                $group_acl = implode(',', $tmp);
            }
        }

        $group_path = ',0,';
        $group_depth = '0';
        if($group_pid > 0){
            $up = $this->getAclGroup(4, array('group_id'=>$group_pid), 'group_path');
            if($up){
                $group_path = $up['group_path'].$group_pid.',';
                $group_depth = substr_count($group_path, ',') - 2;
            }
        }

        $row = array();
        $row['group_name'] = $group_name;
        $row['group_pid'] = $group_pid;
        $row['group_path'] = $group_path;
        $row['group_depth'] = $group_depth;
        $row['group_acl'] = $group_acl;
        $row['st'] = 1;

        $insertID = $this->add($row, array('table'=>getTable('tb_acl_group')));

        return array('st'=>'ok', 'msg'=>$insertID);
    }

    /**
     * 修改分组
     *
     * @param array $datas
     *
     * @return array
     */
    public function editAclGroup($datas)
    {
        $group_id = intval(I('post.group_id'));
        if($group_id < 0){
            return array('st'=>'err', 'msg'=>'参数错误');
        }
        if(isset($datas['group_name']) && $datas['group_name']!=''){
            $group_name = $datas['group_name'];
        }else{
            return array('st'=>'err', 'msg'=>'请输入分组名称');
        }

        if(isset($datas['group_pid']) && intval($datas['group_pid'])>0){
            $group_pid = intval($datas['group_pid']);
        }else{
            $group_pid = 0;
        }

        $group_acl = '';
        if(is_array($datas['auth']) && count($datas['auth'])){
            $tmp = array();
            foreach($datas['auth'] as $v){
                $v = intval($v);
                if($v > 0){
                    $tmp[] = $v;
                }
            }
            if(count($tmp)){
                $group_acl = implode(',', $tmp);
            }
        }

        $group_path = ',0,';
        $group_depth = '0';
        if($group_pid > 0){
            $up = $this->getAclGroup(4, array('group_id'=>$group_pid), 'group_path');
            if($up){
                $group_path = $up['group_path'].$group_pid.',';
                $group_depth = substr_count($group_path, ',') - 2;
            }
        }

        $row = array();
        $row['group_name'] = $group_name;
        $row['group_pid'] = $group_pid;
        $row['group_path'] = $group_path;
        $row['group_depth'] = $group_depth;
        $row['group_acl'] = $group_acl;

        $numRows = $this->save($row, array('table'=>getTable('tb_acl_group'), 'where'=>array('group_id'=>$group_id)));
        if($numRows === false){ return array('st'=>'err', 'msg'=>'没有更新'); }
        else{ return array('st'=>'ok', 'msg'=>$numRows); }
    }

    /**
     * 修改分组
     *
     * @param array $dat 需要更新的数据
     * @param int $group_id 节点id
     *
     * @return array
     */
    public function updateAclGroupByGroupId($dat, $group_id=0)
    {
        $group_id = intval($group_id);
        if($group_id < 1){ return array('st'=>'err', 'msg'=>'参数错误'); }
        $numRows = $this->save($dat, array('table'=>getTable('tb_acl_group'), 'where'=>array('group_id'=>$group_id)));
        if($numRows === false){ return array('st'=>'err', 'msg'=>'没有更新'); }
        else{ return array('st'=>'ok', 'msg'=>$numRows); }
    }

    /**
     * 删除分组
     *
     * @param int $group_id
     *
     * @return array
     */
    public function delAclGroup($group_id = 0)
    {
        $group_id = intval($group_id);
        if($group_id < 1){ return array('st'=>'err', 'msg'=>'参数错误'); }
        //检查有没有子分组
        $ck = $this->getAclGroup(1, array('group_path'=>','.$group_id.','));
        if($ck > 0){ return array('st'=>'err', 'msg'=>'有子菜单子分组，不能删除'); }
        $numRows = $this->delete(array('table'=>getTable('tb_acl_group'), 'where'=>array('group_id'=>$group_id)));
        if($numRows === false){ return array('st'=>'err', 'msg'=>'没有删除'); }
        else{ return array('st'=>'ok', 'msg'=>$numRows); }
    }

    /**
     * 得到用户列表
     *
     * @param int $w
     * @param array $search
     * @param string $fields
     * @param int $page
     * @param int $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getAclAdmin($w=3, $search=null, $fields='a.*', $page=null, $pageSize=null, $by='', $exclude=null)
    {
        $where = ' where 1=1';

        $limit = '';
        if($page && $pageSize){
            $limit = ' limit '.$pageSize.' offset '.($page - 1)*$pageSize;
        }

        $orderby = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderby .= 'a.admin_id desc';
        }else{
            $orderby .= $by;
        }

        if(is_array($search))
        {
            isset($search['admin_id']) && intval($search['admin_id'])>0 && $where .= ' and a.admin_id='.intval($search['admin_id']);
            isset($search['admin_group_id']) && intval($search['admin_group_id'])>0 && $where .= ' and a.admin_group_id='.intval($search['admin_group_id']);
            isset($search['st']) && $search['st']!='' && $where .= " and a.st=".intval($search['st']);
            isset($search['admin_name']) && $search['admin_name']!='' && $where .= " and a.admin_name='".$search['admin_name']."'";
            isset($search['admin_name_like']) && $search['admin_name_like']!='' && $where .= " and a.admin_name like '%".$search['admin_name_like']."%'";
            if(isset($search['root'])){
                if( ! $search['root']){
                    $where .= " and a.admin_id!=1";
                }
            }
        }

        if($exclude){
            $where .= $exclude;
        }

        $sql_ct = "select count(a.admin_id) as tot from ".getTable('tb_acl_admin').' as a '.$where;
        $sql    = 'select '.$fields.' from '.getTable('tb_acl_admin').' as a left join '.getTable('tb_acl_group').' as b on a.admin_group_id=b.group_id '.$where.$orderby;

        //只得到计数
        if($w === 1)
        {
            $ct = $this->query($sql_ct);
            if($ct === false){
                return 0;
            }else{
                return intval($ct[0]['tot']);
            }
        }

        //只得到数据
        if($w === 2)
        {
            return $this->query($sql.$limit);
        }

        //得到计数 & 数据
        if($w === 3)
        {
            $datas = array();
            $ct = $this->query($sql_ct);
            if($ct === false){
                $datas['tot'] = 0;
            }else{
                $datas['tot'] = intval($ct[0]['tot']);
            }
            $datas['datas'] = $this->query($sql.$limit);
            return $datas;
        }

        //得到一条数据
        if($w === 4)
        {
            $rs = $this->query($sql.' limit 1');
            if($rs){
                return array_shift($rs);
            }else{
                return false;
            }
        }

        return false;
    }

    /**
     * 得到用户列表
     *
     * @param int $w
     * @param array $search
     * @param string $fields
     * @param int $page
     * @param int $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getTableAclAdmin($w=3, $search=null, $fields='*', $page=null, $pageSize=null, $by='', $exclude=null)
    {
        $where = ' where 1=1';

        $limit = '';
        if($page && $pageSize){
            $limit = ' limit '.$pageSize.' offset '.($page - 1)*$pageSize;
        }

        $orderby = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderby .= 'admin_id desc';
        }else{
            $orderby .= $by;
        }

        if(is_array($search))
        {
            isset($search['admin_id']) && intval($search['admin_id'])>0 && $where .= ' and admin_id='.intval($search['admin_id']);
            isset($search['admin_group_id']) && intval($search['admin_group_id'])>0 && $where .= ' and admin_group_id='.intval($search['admin_group_id']);
            isset($search['st']) && $search['st']!='' && $where .= " and st=".intval($search['st']);
            isset($search['admin_name']) && $search['admin_name']!='' && $where .= " and admin_name='".$search['admin_name']."'";
            isset($search['admin_name_like']) && $search['admin_name_like']!='' && $where .= " and admin_name like '%".$search['admin_name_like']."%'";
            if(isset($search['root'])){
                if( ! $search['root']){
                    $where .= " and admin_id!=1";
                }
            }
        }

        if($exclude){
            $where .= $exclude;
        }

        $sql_ct = "select count(admin_id) as tot from ".getTable('tb_acl_admin').$where;
        $sql    = 'select '.$fields.' from '.getTable('tb_acl_admin').$where.$orderby;

        //只得到计数
        if($w === 1)
        {
            $ct = $this->query($sql_ct);
            if($ct === false){
                return 0;
            }else{
                return intval($ct[0]['tot']);
            }
        }

        //只得到数据
        if($w === 2)
        {
            return $this->query($sql.$limit);
        }

        //得到计数 & 数据
        if($w === 3)
        {
            $datas = array();
            $ct = $this->query($sql_ct);
            if($ct === false){
                $datas['tot'] = 0;
            }else{
                $datas['tot'] = intval($ct[0]['tot']);
            }
            $datas['datas'] = $this->query($sql.$limit);
            return $datas;
        }

        //得到一条数据
        if($w === 4)
        {
            $rs = $this->query($sql.' limit 1');
            if($rs){
                return array_shift($rs);
            }else{
                return false;
            }
        }

        return false;
    }

    /**
     * 添加用户
     *
     * @param $post
     *
     * @return array
     */
    public function addAclAdmin($post)
    {
        $row = array();

        if(isset($post['admin_group_id']) && intval($post['admin_group_id'])>0){
            $row['admin_group_id'] = intval($post['admin_group_id']);
        }else{
            return array('st'=>'err', 'msg'=>'请选择分组');
        }

        if(isset($post['admin_name']) && $post['admin_name']!=''){
            //检查用户名是否已经存在
            $ck = $this->getAclAdmin(1, array('admin_name'=>$post['admin_name']));
            if($ck > 0){ return array('st'=>'err', 'msg'=>'请用户名已经存在'); }
            $row['admin_name'] = $post['admin_name'];
        }else{
            return array('st'=>'err', 'msg'=>'请输入用户名');
        }

        if(isset($post['admin_password']) && $post['admin_password']!=''){
            $row['admin_password'] = my_encode($post['admin_password']);
        }else{
            return array('st'=>'err', 'msg'=>'请输入密码');
        }

        if(is_array($post['auth']) && count($post['auth'])){
            $auth = array();
            foreach($post['auth'] as $v){
                $v = intval($v);
                if($v > 0){
                    $auth[] = $v;
                }
            }
            if(count($auth)){
                sort($auth);
                $row['admin_acl'] = implode(',', $auth);
            }else{
                $row['admin_acl'] = '';
            }
        }else{
            $row['admin_acl'] = '';
        }

        $row['ctime'] = time();
        $row['st'] = 1;

        $insertID = $this->add($row, array('table'=>getTable('tb_acl_admin')));

        return array('st'=>'ok', 'msg'=>$insertID);
    }

    /**
     * 修改用户
     *
     * @param $post
     *
     * @return array
     */
    public function editAclAdmin($post)
    {
        $row = array();

        $admin_id = intval(I('admin_id'));
        if($admin_id < 2){
            return array('st'=>'err', 'msg'=>'参数错误');
        }

        if(isset($post['admin_group_id']) && intval($post['admin_group_id'])>0){
            $row['admin_group_id'] = intval($post['admin_group_id']);
        }

        if(isset($post['admin_password']) && $post['admin_password']!=''){
            $row['admin_password'] = my_encode($post['admin_password']);
        }

        if(is_array($post['auth']) && count($post['auth'])){
            $auth = array();
            foreach($post['auth'] as $v){
                $v = intval($v);
                if($v > 0){
                    $auth[] = $v;
                }
            }
            if(count($auth)){
                sort($auth);
                $row['admin_acl'] = implode(',', $auth);
            }else{
                $row['admin_acl'] = '';
            }
        }else{
            $row['admin_acl'] = '';
        }

        $numRows = $this->save($row, array('table'=>getTable('tb_acl_admin'), 'where'=>array('admin_id'=>$admin_id)));
        if($numRows === false){ return array('st'=>'err', 'msg'=>'没有更新'); }
        else{ return array('st'=>'ok', 'msg'=>$numRows); }
    }

    /**
     * 修改用户
     *
     * @param array $dat 需要更新的数据
     * @param int $admin_id
     *
     * @return array
     */
    public function updateAclAdminByAdminId($dat, $admin_id=0)
    {
        $admin_id = intval($admin_id);
        if($admin_id < 1){ return array('st'=>'err', 'msg'=>'参数错误'); }
        $numRows = $this->save($dat, array('table'=>getTable('tb_acl_admin'), 'where'=>array('admin_id'=>$admin_id)));
        if($numRows === false){ return array('st'=>'err', 'msg'=>'没有更新'); }
        else{ return array('st'=>'ok', 'msg'=>$numRows); }
    }

    /**
     * 更改密码
     *
     * @param array $post
     *
     * @return array
     */
    public function changePassword($post)
    {
        $formpassword = '';
        if(isset($post['formpassword']) && $post['formpassword']!=''){
            $formpassword = $post['formpassword'];
        }else{
            return array('st'=>'err', 'msg'=>'请输入原密码');
        }

        $admin_password = '';
        if(isset($post['admin_password']) && $post['admin_password']!=''){
            $admin_password = $post['admin_password'];
        }else{
            return array('st'=>'err', 'msg'=>'请输入新密码');
        }

        $rppassword = '';
        if(isset($post['rppassword']) && $post['rppassword']!=''){
            $rppassword = $post['rppassword'];
        }else{
            return array('st'=>'err', 'msg'=>'请再次输入新密码');
        }

        if($rppassword != $admin_password){ return array('st'=>'err', 'msg'=>'两次输入的新密码不同'); }

        //检查原密码是否正确
        $ck = $this->getAclAdmin(4, array('admin_id'=>session('admin_id'), 'root'=>session('root')), 'admin_password');
        if($ck){
            if($ck['admin_password'] != my_encode($formpassword)){ return array('st'=>'err', 'msg'=>'pwderr'); }
            else{
                $this->updateAclAdminByAdminId(array('admin_password'=>my_encode($admin_password)), session('admin_id'));
                return array('st'=>'ok', 'msg'=>'ok');
            }
        }else{
            return array('st'=>'err', 'msg'=>'无此用户');
        }
    }
}